﻿using IFRS.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace IFRS.DataAccess.DAL
{
    public class SPR_FAIRVALUEOPTION
    {
        public DataTable SEARCH_ALL(FairValueOption fairValueOption, Paging paging, bool IFRS_ALLBRANCH, string sIFRS_COST_CENTER, ref int iRecordCount)
        {
            DataTable dt = new DataTable();
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@RecordCount", SqlDbType.Int));
                lstParams[0].Direction = ParameterDirection.Output;
                lstParams.Add(new SqlParameter("@PageIndex", paging.PageIndex));
                lstParams.Add(new SqlParameter("@PageSize", paging.PageSize));
                lstParams.Add(new SqlParameter("@Sorting", paging.Sorting));
                lstParams.Add(new SqlParameter("@isAllBranch", IFRS_ALLBRANCH));
                lstParams.Add(new SqlParameter("@sCostCenter", sIFRS_COST_CENTER));
                lstParams.Add(new SqlParameter("@TransactionDate", fairValueOption.TransactionDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@SourceDataID", fairValueOption.SourceDataID.AsDBValue()));
                lstParams.Add(new SqlParameter("@AccountNo", fairValueOption.AccountNo.AsDBValue()));
                dt = Utility.StoredProcDataTable("SPR_FAIRVALUEOPTION_SEARCHALL", lstParams.ToArray());

                iRecordCount = (int)lstParams[0].Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dt;
        }

        public DataTable SEARCH_BYID(int FairValueOptionID)
        {
            DataTable dt = new DataTable();
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@FairValueOptionID", FairValueOptionID));
                dt = Utility.StoredProcDataTable("SPR_FAIRVALUEOPTION_SEARCHBYID", lstParams.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dt;
        }

        public int INSERT(FairValueOption fairValueOption)
        {
            int iNum = 0;
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();

            try
            {
                lstParams.Add(new SqlParameter("@FairValueOptionID", SqlDbType.Int));
                lstParams[0].Direction = ParameterDirection.Output;
                lstParams.Add(new SqlParameter("@TransactionDate", fairValueOption.TransactionDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@SourceDataID", fairValueOption.SourceDataID.AsDBValue()));
                lstParams.Add(new SqlParameter("@AccountNo", fairValueOption.AccountNo.AsDBValue()));
                lstParams.Add(new SqlParameter("@SourceOfData", fairValueOption.SourceOfData.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedDate", fairValueOption.CreatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedBy", fairValueOption.CreatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedCostCenter", fairValueOption.CreatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedBranch", fairValueOption.CreatedBranch.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedDate", fairValueOption.UpdatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBy", fairValueOption.UpdatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", fairValueOption.UpdatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBranch", fairValueOption.UpdatedBranch.AsDBValue()));

                Utility.ExecuteStoredProc("SPR_FAIRVALUEOPTION_INSERT", lstParams.ToArray());

                iNum = (int)lstParams[0].Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }

        public int UPDATE(FairValueOption fairValueOption)
        {
            int iNum = 0;
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@FairValueOptionID", fairValueOption.FairValueOptionID.AsDBValue()));
                lstParams.Add(new SqlParameter("@TransactionDate", fairValueOption.TransactionDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@SourceDataID", fairValueOption.SourceDataID.AsDBValue()));
                lstParams.Add(new SqlParameter("@AccountNo", fairValueOption.AccountNo.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedDate", fairValueOption.UpdatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBy", fairValueOption.UpdatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", fairValueOption.UpdatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBranch", fairValueOption.UpdatedBranch.AsDBValue()));

                iNum = (int)Utility.ExecuteStoredProc("SPR_FAIRVALUEOPTION_UPDATE", lstParams.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }

        public int DELETE(FairValueOption fairValueOption)
        {
            int iNum = 0;
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@FairValueOptionID", fairValueOption.FairValueOptionID.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedDate", fairValueOption.UpdatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBy", fairValueOption.UpdatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", fairValueOption.UpdatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBranch", fairValueOption.UpdatedBranch.AsDBValue()));
                iNum = (int)Utility.ExecuteStoredProc("SPR_FAIRVALUEOPTION_DELETE", lstParams.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }
    }
}